<?php

declare(strict_types=1);

namespace App\Service\Business;

use App\Service\Access\MenuAccess;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
 * 菜单-业务类
 */
class MenuBusiness extends BaseBusiness
{
        #[Inject]
        protected MenuAccess $MenuAccess;

        /**
         * 创建
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         */
        public function create(): array
        {
                $intPid = intval($this->request->input('pid', 0));
                $intLevel = $intPid ? (intval($this->MenuAccess->value(['wheres' => ['menuId' => $intPid],
                                'column' => 'level'])) + 1) : 1;
                $arrInsert = [
                        'menuSnowflakeId' => snowflakeId(),
                        'pid' => $intPid,
                        'level' => $intLevel,
                        'name' => $this->request->input('name', ''),
                        'title' => $this->request->input('title', ''),
                        'route' => $this->request->input('route', ''),
                        'path' => $this->request->input('path', ''),
                        'activePath' => $this->request->input('activePath', ''),
                        'link' => $this->request->input('link', ''),
                        'icon' => $this->request->input('icon', ''),
                        'permissionMark' => $this->request->input('permissionMark', ''),
                        'queryParams' => $this->request->input('queryParams', '{}'),
                        'pathParams' => $this->request->input('pathParams', '{}'),
                        'belongTo' => $this->request->input('belongTo'),
                        'method' => $this->request->input('method'),
                        'status' => $this->request->input('status'),
                        'type' => $this->request->input('type'),
                        'isHide' => $this->request->input('isHide'),
                        'isHideTab' => $this->request->input('isHideTab'),
                        'isKeepAlive' => $this->request->input('isKeepAlive'),
                        'isFixedTab' => $this->request->input('isFixedTab'),
                        'isIframe' => $this->request->input('isIframe'),
                        'isFullPage' => $this->request->input('isFullPage'),
                        'isBadge' => $this->request->input('isBadge'),
                        'badgeText' => $this->request->input('badgeText', ''),
                        'sort' => $this->request->input('sort', DEFAULT_SORT),
                        'createTime' => $this->request->getAttribute('timestamp'),
                ];
                $intMenuId = $this->MenuAccess->insertGetId($arrInsert);
                if (!$intMenuId) return businessError();
                $this->createOperateLog([], $arrInsert);
                return businessSuccess(['menuId' => $intMenuId]);
        }

        /**
         * 更新
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         */
        public function update(): array
        {
                $intPid = intval($this->request->input('pid', 0));
                $intLevel = 1;
                if ($intPid) {
                        $intLevel = intval($this->MenuAccess->value(['wheres' => ['menuId' => $intPid], 'column' => 'level'])) + 1;
                }
                $arrData = $this->MenuAccess->find($this->request->input('menuId'), [
                        'pid', 'level', 'name', 'title', 'route', 'path', 'activePath', 'link', 'icon',
                        'permissionMark', 'queryParams', 'pathParams', 'method', 'belongTo', 'status', 'type',
                        'isHide', 'isHideTab', 'isKeepAlive', 'isFixedTab', 'isIframe', 'isFullPage', 'isBadge',
                        'badgeText', 'sort', 'updateTime']);
                $arrUpdate = [
                        'menuId' => $this->request->input('menuId'),
                        'pid' => $intPid,
                        'level' => $intLevel,
                        'name' => $this->request->input('name', ''),
                        'title' => $this->request->input('title', ''),
                        'route' => $this->request->input('route', ''),
                        'path' => $this->request->input('path', ''),
                        'activePath' => $this->request->input('activePath', ''),
                        'link' => $this->request->input('link', ''),
                        'icon' => $this->request->input('icon', ''),
                        'permissionMark' => $this->request->input('permissionMark', ''),
                        'queryParams' => $this->request->input('queryParams', '{}'),
                        'pathParams' => $this->request->input('pathParams', '{}'),
                        'belongTo' => $this->request->input('belongTo'),
                        'method' => $this->request->input('method'),
                        'status' => $this->request->input('status'),
                        'type' => $this->request->input('type'),
                        'isHide' => $this->request->input('isHide'),
                        'isHideTab' => $this->request->input('isHideTab'),
                        'isKeepAlive' => $this->request->input('isKeepAlive'),
                        'isFixedTab' => $this->request->input('isFixedTab'),
                        'isIframe' => $this->request->input('isIframe'),
                        'isFullPage' => $this->request->input('isFullPage'),
                        'isBadge' => $this->request->input('isBadge'),
                        'badgeText' => $this->request->input('badgeText', ''),
                        'sort' => $this->request->input('sort', DEFAULT_SORT),
                        'updateTime' => $this->request->getAttribute('timestamp'),
                ];
                $intEffectRow = $this->MenuAccess->updateById($arrUpdate);
                if (!$intEffectRow) return businessError();
                $this->createOperateLog($arrData, $arrUpdate);
                return businessSuccess();
        }

        /**
         * 分页
         * @param array $inject
         * @return array
         */
        public function paginate(array $inject = []): array
        {
                $arrQueryResult = $this->MenuAccess->paginate($inject);
                $arrResult = [
                        'page' => $arrQueryResult['current_page'],
                        'limit' => $arrQueryResult['per_page'],
                        'total' => $arrQueryResult['total'],
                        'list' => $arrQueryResult['data'],
                ];
                return businessSuccess($arrResult);
        }

        /**
         * 查询是否存在
         * @param array $inject
         * @return bool
         */
        public function exists(array $inject = []): bool
        {
                return $this->MenuAccess->exists($inject);
        }
}